package com.likeycy.my;

import com.likeycy.my.entity.Person;
import com.likeycy.my.printer.BinaryTrees;
import com.likeycy.my.tree.MyBinarySearchTree;

import java.util.Comparator;
import java.util.List;

/**
 * @ClassName: TestMain
 * @Description: 测试自定义实现的二叉树
 * @Author: sodagreen
 * @Date: 2021/3/5 13:43
 * @Version: 1.0
 */
public class TestMain {

    public static Integer[] arrayInt = new Integer[] {
            7, 4, 2, 1, 3, 5, 9, 8, 11, 10, 12, 6
    };

    public static void main(String[] args) {
        /*testMyBinarySearchTree1();
        testMyBinarySearchTree2();
        testMyBinarySearchTree3();*/

        // 前序遍历测试
//        testMyBinarySearchTreeByPreorderTraversal();

        // 中序遍历测试
//        testMyBinarySearchTreeByInorderTraversal();

        // 后序遍历测试
//        testMyBinarySearchTreeByPostorderTraversal();

        // 层序遍历
//        testMyBinarySearchTreeByLevelOrderTraversal();

        testMyBinarySearchTreeByRemoveMethod();
    }

    public static void testMyBinarySearchTree1() {
        Integer[] arrayInt = new Integer[] {
                7, 4, 9, 2, 5, 8, 11, 3, 12, 1
        };
        com.likeycy.my.tree.MyBinarySearchTree<Integer> binarySearchTree = new com.likeycy.my.tree.MyBinarySearchTree<>();
        for (Integer integer : arrayInt) {
            binarySearchTree.add(integer);
        }

        // 控制台打印二叉树
        BinaryTrees.println(binarySearchTree);
    }

    public static void testMyBinarySearchTree2() {
        Integer[] arrayInt = new Integer[] {
                7, 4, 9, 2, 5, 8, 11, 3, 12, 1
        };
        com.likeycy.my.tree.MyBinarySearchTree<Person> binarySearchTree = new com.likeycy.my.tree.MyBinarySearchTree<>();
        for (Integer integer : arrayInt) {
            Person person = new Person();
            person.setAge(integer);
            binarySearchTree.add(person);
        }

        // 控制台打印二叉树
        BinaryTrees.println(binarySearchTree);

    }

    public static void testMyBinarySearchTree3() {
        com.likeycy.my.tree.MyBinarySearchTree<Object> binarySearchTree = new com.likeycy.my.tree.MyBinarySearchTree<>();
        for (int i = 0; i < 30; i++) {
            binarySearchTree.add((int)(Math.random() * 100));
        }
        String str = BinaryTrees.printString(binarySearchTree);
        str += "\n\n";
        // 打印二叉树到文件查看。 true参数表示追加到文件末尾，不进行覆盖
        //Files.writeToFile("E:/RandomNumberTreePrint.txt", str, true);
    }

    /**
     * 前序遍历测试
     */
    public static void testMyBinarySearchTreeByPreorderTraversal() {
        com.likeycy.my.tree.MyBinarySearchTree<Person> bst = new com.likeycy.my.tree.MyBinarySearchTree<>();
        for (Integer integer : arrayInt) {
            Person person = new Person();
            person.setAge(integer);
            bst.add(person);
        }

        // 控制台打印二叉树
        BinaryTrees.println(bst);
        // 前序遍历方式
        List<Person> list = bst.preorderTraversal();
        list.forEach(System.out::println);
    }

    /**
     * 中序遍历测试
     */
    public static void testMyBinarySearchTreeByInorderTraversal() {

        com.likeycy.my.tree.MyBinarySearchTree<Person> bst = new com.likeycy.my.tree.MyBinarySearchTree<>();
        for (Integer integer : arrayInt) {
            Person person = new Person();
            person.setAge(integer);
            bst.add(person);
        }

        // 控制台打印二叉树
        BinaryTrees.println(bst);
        // 中序遍历方式
        List<Person> list = bst.inorderTraversal();
        list.forEach(System.out::println);
    }

    /**
     * 后序遍历测试
     */
    public static void testMyBinarySearchTreeByPostorderTraversal() {

        com.likeycy.my.tree.MyBinarySearchTree<Person> bst = new com.likeycy.my.tree.MyBinarySearchTree<>();
        for (Integer integer : arrayInt) {
            Person person = new Person();
            person.setAge(integer);
            bst.add(person);
        }

        // 控制台打印二叉树
        BinaryTrees.println(bst);
        // 后序遍历方式
        List<Person> list = bst.postorderTraversal()
                ;
        list.forEach(System.out::println);
    }

    /**
     * 层序遍历测试
     */
    public static void testMyBinarySearchTreeByLevelOrderTraversal() {

        com.likeycy.my.tree.MyBinarySearchTree<Person> bst = new com.likeycy.my.tree.MyBinarySearchTree<>();
        for (Integer integer : arrayInt) {
            Person person = new Person();
            person.setAge(integer);
            bst.add(person);
        }

        // 控制台打印二叉树
        BinaryTrees.println(bst);
        // 中序遍历方式
        List<Person> list = bst.levelOrderTraversal();
                ;
        list.forEach(System.out::println);

        bst.levelOrder(new com.likeycy.my.tree.MyBinarySearchTree.Visitor<Person>() {
            @Override
            public boolean visit(Person element) {
                System.out.print("_" + element + "_");
                if (element.getAge() == 5) {
                    return true;
                }
                return false;
            }
        });

        System.out.println();

        bst.preorder(new com.likeycy.my.tree.MyBinarySearchTree.Visitor<Person>() {
            @Override
            public boolean visit(Person element) {
                System.out.print("_" + element + "_");
                if (element.getAge() == 5) {
                    return true;
                }
                return false;
            }
        });
        System.out.println();

        bst.inorder(new com.likeycy.my.tree.MyBinarySearchTree.Visitor<Person>() {
            @Override
            public boolean visit(Person element) {
                System.out.print("_" + element + "_");
                if (element.getAge() == 5) {
                    return true;
                }
                return false;
            }
        });
        System.out.println();

        bst.postorder(new com.likeycy.my.tree.MyBinarySearchTree.Visitor<Person>() {
            @Override
            public boolean visit(Person element) {
                System.out.print("_" + element + "_");
                if (element.getAge() == 5) {
                    return true;
                }
                return false;
            }
        });
    }

    public static void testMyBinarySearchTreeByRemoveMethod() {

        com.likeycy.my.tree.MyBinarySearchTree<Person> bst = new MyBinarySearchTree<>();
        for (Integer integer : arrayInt) {
            Person person = new Person();
            person.setAge(integer);
            bst.add(person);
        }

        // 控制台打印二叉树
        BinaryTrees.println(bst);
        // 后序遍历方式
        List<Person> list = bst.postorderTraversal();
        bst.remove(new Person(9));
        System.out.println();
        BinaryTrees.println(bst);
    }



    private static class PersonComparator1 implements Comparator<Person> {
        @Override
        public int compare(Person o1, Person o2) {
            return o1.getAge() - o2.getAge();
        }
    }

    private static class PersonComparator2 implements Comparator<Person> {
        @Override
        public int compare(Person o1, Person o2) {
            return o2.getAge() - o1.getAge();
        }
    }
}
